package com.ydgk.dao;

import com.ydgk.bean.Supplier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class SupplierDao extends BaseDao<Supplier> {
    @Override
    public int save(Supplier supplier) {
        return template.update("insert into supplier values(?,?,?,?)",
                supplier.getSid(),
                supplier.getSname(),
                supplier.getSaddress(),
                supplier.getStel());
    }

    @Override
    public int updateById(Supplier supplier) {
        return template.update("update supplier set sname = ?,saddress = ?,stel = ? where sid = ?",
                supplier.getSname(),
                supplier.getSaddress(),
                supplier.getStel(),
                supplier.getSid());
    }

    @Override
    public int deleteById(int id) {
        try {
            return template.update("delete from supplier where sid = ?", id);
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public List<Supplier> findAll() {
        return template.query("select *from supplier",new BeanPropertyRowMapper<Supplier>(Supplier.class));
    }

    @Override
    public Supplier findById(int id) {
        String sql = "select * from supplier where sid =?" ;
        List<Supplier> list = template.query(sql, new BeanPropertyRowMapper<Supplier>(Supplier.class),id);
        return list.size() > 0 ? list.get(0) : null ;
    }

    @Override
    public List<Supplier> paging(int pageNumber) {
        return template.query("select * from supplier order by sid desc limit ?,?",new BeanPropertyRowMapper<Supplier>(Supplier.class) , (pageNumber-1)*pageCount ,pageCount);
    }

    public int findAllNumber(){
        String sql = "select count(*) from supplier";
        long t = template.queryForObject(sql, Long.class);
        int total = (int)t;//将long类型转换成int类型
        return total;
    }
}
